package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.common.model.ResultModel;
import com.bifrost.entity.User;
import com.bifrost.model.brand.ProductBrandModel;
import com.bifrost.service.AdminUserService;
import com.bifrost.service.BrandService;
import com.bifrost.service.SapDataSyncService;
import com.bifrost.validator.group.IsAdd;
import com.bifrost.validator.group.IsUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 里昂
 * @com.bifrost.admin.controller
 * @wechat-mall
 * @lee Create Class on 2018/4/4
 */
@RestController
@Api("/后台品牌相关接口")
@RequestMapping("/admin/brand")
@Validated
public class AdminBrandController {

    @Autowired
    BrandService brandService;
    @Autowired
    AdminUserService userService;
    @Autowired
    SapDataSyncService syncService;
    @GetMapping("/page")
    @ApiOperation(value = "/分页获取所有名牌",notes = "获取所有品牌" )
    @ApiImplicitParams({@ApiImplicitParam(name = "page" , value = "当前页" , dataType = "int" , paramType = "query" , required = true , defaultValue = "1"),
            @ApiImplicitParam(name = "size" , value = "页数" , dataType = "int" , paramType = "query" , defaultValue = "8"),
            @ApiImplicitParam(name = "key" , value = "品牌名称" , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "owner" , value = "品牌出品公司" , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "externalIds" , value = "SAP编码" , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "origin" , value = "生产地" , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "merchantId",value = "门店编码",dataType = "Long",paramType = "query")})
    public ResultModel page(@RequestParam(value = "page",defaultValue = "1")Integer page , @RequestParam(value = "size",defaultValue = "8") Integer size , String key , String owner, String origin , String externalIds){
            return ResultModel.ok().put("result",brandService.findAllPage(page, size, key, owner, origin, externalIds));
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有名牌" , notes = "获取品牌数据不分页")
    public ResultModel findAll() {
            List brands = brandService.findAll();
            return ResultModel.ok().put("result",brands);
    }

    @GetMapping("/get")
    @ApiOperation(value = "/展示品牌")
    @ApiImplicitParam(name = "brandId" , value = "品牌Id" , required = true ,dataType = "Long" , paramType = "query")
    public ResultModel findOne(@RequestParam @NotNull(message = "品牌不能为空") Long brandId){
        return ResultModel.ok().put("result",brandService.findOne(brandId));
    }

    @PostMapping("/add")
    @ApiOperation(value = "/添加品牌数据")
    public ResultModel addBrand(@CurrentAdmin Long userId , @RequestBody @Validated(IsAdd.class)  ProductBrandModel brandModel){
            brandService.addBrand(userService.findById(userId),brandModel);
            return ResultModel.ok("品牌数据保存成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "/修改品牌数据")
    public ResultModel updateBrand(@CurrentAdmin Long userId , @RequestBody @Validated(IsUpdate.class) ProductBrandModel brandModel){
            brandService.addBrand(userService.findById(userId) , brandModel);
            return ResultModel.ok("品牌数据保存成功");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "/删除品牌" , notes = "支持批量删除品牌")
    public ResultModel delete(@CurrentAdmin Long userId , @RequestParam @NotEmpty(message = "品牌不能为空") List<Long> brandIds){

            brandService.delete(userService.findById(userId) , brandIds);
            return ResultModel.ok();
    }

    @GetMapping("/sync/now")
    public ResultModel delete(){
        syncService.syncSapBrand();
        return ResultModel.ok();
    }
}
